<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Auto Trading</title>
  <!-- https://electronjs.org/docs/tutorial/security#csp-meta-tag -->
  <!-- <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" /> -->
  <!-- <meta http-equiv="Content-Security-Policy" content="font-src 'self' data: http://*; default-src *; style-src 'self' http://* 'unsafe-inline'; script-src 'self' http://* https://* 'unsafe-inline' 'unsafe-eval'" /> -->
  <style>
  </style>
</head>

<body>

  <h3>缩小当前窗口， 按下 Alt + F6 开始采集鼠标位置； 再次按下 Alt + F6 保存采集记录。 按 Alt + F7 测试最后采集的情况。 </h3>
  <h3> USD_XAU : <span id="USD_XAU_PRICE"></span> </h3>
  <section>
    <button id="ALTER">修改</button>
    <button id="VALIDATE">校验</button>
    <!-- <label for="TRIGGER_SWITCH">后台执行<input id="TRIGGER_SWITCH" type="checkbox"></label> -->
    <br>
    <textarea id="SCRIPT" placeholder="填写触发代码" rows="20" cols="70"></textarea>
  </section>
  <script src="./src/front/ws/goldPrice.js"></script>
  <script src="./src/front/ws/globalContext.js"></script>
  <script src="./src/front/view/globalContext.js"></script>
  <script src="./src/front/view/switch.js"></script>
  <script src="./src/front/view/util.js"></script>
  <script src="./src/front/view/price.js"></script>
  <script>
    // const TRIGGER_SWITCH = $ID('TRIGGER_SWITCH');
    const ALTER = $ID('ALTER');
    const VALIDATE = $ID('VALIDATE');
    const FIRE = $ID('FIRE');
    const SCRIPT = $ID('SCRIPT');
    const FIX_TRIGGER_ID = 1;
    const FIX_MOUSE_ID = -1;
    const DEFAULT_TRADE_FUNCITON = `
    function tradeTest(){
      return (new Date()).getSeconds() % 10 === 0;
    };
    tradeTest();
    `;

    const initTriggerScript = () => {
      getJson(`/action/trigger/${FIX_TRIGGER_ID}`, row => {
        console.log(row);

        if (row.id !== FIX_TRIGGER_ID) {
          return putData(`/action/trigger`, { note: '', script: btoa(DEFAULT_TRADE_FUNCITON) }, (rtn) => {
            if (!rtn) alert('初始化错误，系统无法正常运行。');
            initTriggerScript();
          });
        }

        try {
          SCRIPT.value = atob(row.script);
        } catch (err) {
          alert('代码解析错误');
        }
      });
    }

    initTriggerScript();

    /*
    const initTriggerSwitch = () => {
      if (!!isGlobalContextOpen) {
        TRIGGER_SWITCH.checked = globalContext.TriggerSwitch;
      } else {
        setTimeout(() => initTriggerSwitch(), 1000);
      }
    }

    initTriggerSwitch();

    TRIGGER_SWITCH.addEventListener('change', () => {
      triggerSwitch();
    });
    */

    ALTER.addEventListener('click', () => {
      if (!confirm('确认修改?')) return;
      let script = btoa(SCRIPT.value);
      patchData(`/action/trigger/${FIX_TRIGGER_ID}`, { "note": 'FIX note', "script": script }, (rtn) => {
        if (!rtn) alert('提交的代码不规范，请检查代码是否正确。');
      });
    });

    VALIDATE.addEventListener('click', () => {
      let script = btoa(SCRIPT.value);
      postData('/action/trigger/vali', { script }, rtn => {
        console.log('validate:', rtn.flag);
        alert(rtn.flag ? '脚本校验通过' : '脚本校验失败');
      });
    });

  </script>
</body>

</html>